//
//  LSDScrollView.m
//  UIPlus_0622_LSDScrollerView
//
//  Created by xalo on 16/6/22.
//  Copyright © 2016年 xalo. All rights reserved.
//

#import "LSDScrollView.h"
#define Kwidh self.frame.size.width
#define kHeigth self.frame.size.height

@interface LSDScrollView()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scrollView;    //滚动视图
@property (nonatomic,strong)UIImageView *leftImageView;  //第一张图
@property (nonatomic,strong)UIImageView *rightImageView; //第二张图
@property (nonatomic,strong)UIPageControl *pageControl;  //小圆点
@property (nonatomic,assign)NSInteger index;             //图片下标
@property (nonatomic,strong)NSTimer *timer;              //定时器


@end

@implementation LSDScrollView
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.delegate = self;
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Kwidh,kHeigth)];
        _scrollView.bounces = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentSize = CGSizeMake(Kwidh*2, kHeigth);
        self.scrollView.delegate = self;
    }
    return _scrollView;
}
- (UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Kwidh, kHeigth)];
        //
        [_leftImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
    }
    return _leftImageView;
}
- (UIImageView *)rightImageView{
    if (!_rightImageView) {
       _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(Kwidh, 0, Kwidh, kHeigth)];

       [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
 
    }
    return _rightImageView;
}
- (UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(Kwidh/2-50, 180, 100, 100)];
        
        _pageControl.numberOfPages = self.imageArray.count;
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
        
    }
    return _pageControl;
}

- (NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray
{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.index = 1;
        self.imageArray = imageArray;
        [self creatUI];
    }
    return self;
}

//创建滚动视图
- (void)creatUI{
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
    
    //延迟一秒播放
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.timer fire];
    });
}
//定时器事件
- (void)timerAction{
    [self.scrollView setContentOffset:CGPointMake(Kwidh, 0) animated:YES];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == Kwidh) {
        scrollView.contentOffset = CGPointZero;//偏移量置0
        
        self.pageControl.currentPage = self.index;
        
        self.index++;
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.rightImageView.image;
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
        
    }
}











@end
